package com.community.backend.controller;

import com.community.backend.context.BaseContext;
import com.community.backend.pojo.Admin;
import com.community.backend.pojo.dto.UserQueryDTO;
import com.community.backend.pojo.vo.AdminLoginVO;
import com.community.backend.pojo.vo.UserRoomVO;
import com.community.backend.pojo.dto.LoginDTO;
import com.community.backend.pojo.User;
import com.community.backend.pojo.dto.UserDTO;
import com.community.backend.properties.JwtProperties;
import com.community.backend.service.UserService;
import com.community.backend.utils.JwtUtil;
import com.community.backend.utils.PageResult;
import com.community.backend.utils.Result;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;
    private final JwtProperties jwtProperties;

    public UserController(JwtProperties jwtProperties) {
        this.jwtProperties = jwtProperties;
    }

    /**
     * 搜索用户,在物业管理添加使用
     */
    @GetMapping("/room/search")
    public Result<List<UserRoomVO>> searchRoom(UserDTO userDTO) {
        log.info("搜索用户信息：{}", userDTO);
        List<UserRoomVO> list = userService.searchRoom(userDTO);
        return Result.success(list);
    }
    /**
     * ⚠️ 安全建议：
     * 不要明文存储密码，应使用 BCrypt 或 SHA-256 加盐加密。
     * 建议登录时只查用户名，再比对密码，避免 SQL 注入和逻辑泄露。
     * @param loginDTO
     * @return
     */
//    @PostMapping("/login")
//    public Result login(@RequestBody LoginDTO loginDTO) {
//        Admin admin = userService.login(loginDTO);
//        System.out.println("登录用户信息"+admin);
//        if (admin == null || admin.getId() == null) {
//            return Result.error("认证失败");
//        }
//        HashMap<String, Object> claims = new HashMap<>();
//        claims.put("adminId",admin.getId());
//        String token = JwtUtil.createJWT(jwtProperties.getAdminSecretKey(),jwtProperties.getAdminTtl(),claims);
//        AdminLoginVO adminLoginVO = new AdminLoginVO();
//        adminLoginVO.setId(admin.getId());
//        adminLoginVO.setToken(token);
//        System.out.println("登录admin"+admin);
//        return Result.success(adminLoginVO);
//    }
    @GetMapping("/page")
    public Result<PageResult> userByPage(UserQueryDTO userQueryDTO, HttpServletRequest  request) {
        System.out.println("请求头里token："+request.getHeader("Authorization"));
        System.out.println("userQueryDTO"+userQueryDTO);
        PageResult pageResult = userService.userByPage(userQueryDTO);
        return Result.success(pageResult);
    }
    @PutMapping
    public Result userUpdate(@RequestBody User user) {
        userService.update(user);
        return Result.success();
    }
    @PutMapping("/enable/{id}")
    public Result enable(@PathVariable String id) {
        User user = new User();
        user.setId(id);
        user.setStatus("启用");
        userService.update(user);
        return Result.success();
    }
    @PutMapping("/resetPwd/{id}")
    public Result resetPwd(@PathVariable String id) {
        User user = new User();
        user.setId(id);
        user.setPassword("123456");
        userService.update(user);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result getById(@PathVariable String id) {
        User user = userService.selectById(id);
        return Result.success(user);
    }
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable String id) {
         userService.deleteById(id);
         return Result.success();
    }
    @PostMapping
    public Result userAdd(@RequestBody User user) {
        userService.addUser(user);
        return Result.success();

    }
}
